<template>
  <el-popover ref="popoverRef" trigger="click" placement="bottom-start" popper-class="p0" :width="240" @show="showList" @hide="hideList">
    <template #reference>
      <div class="search-item" :class="{ active: visible, isSelect: !!selectText }" @click="showList">
        <span class="label">{{ formItem.props?.label }}<span v-if="selectText">:</span></span>
        <span class="text">{{ selectText }}</span>
        <el-icon class="array-icon" :size="10" color="#999">
          <svg-icon name="ep:caret-bottom" />
        </el-icon>
        <el-icon class="delete-icon" :size="10" color="#999" @click.stop="clear">
          <svg-icon name="ep:circle-close-filled" />
        </el-icon>
      </div>
    </template>

    <div class="select-list-wrap">
      <div class="select-list">
        <div v-for="item in localOptions" :key="item.value" :class="{ active: attr.multiple && Array.isArray(selected) ? selected.includes(item.value) : selected === item.value }" class="select-item" @click="selectItem(item)">
          <span class="item-text">{{ item.label }}</span>
          <el-icon :size="12" color="#247FFC">
            <svg-icon name="ep:check" />
          </el-icon>
        </div>
      </div>

      <div v-if="attr.multiple" class="select-footer">
        <el-button :auto-insert-space="false" type="primary" size="small" style="height: 28px;" @click="sureSelect">确定</el-button>
      </div>
    </div>
  </el-popover>
</template>

<script setup>
import { computed, watch } from 'vue'

const props = defineProps({
  fields: [String, Number, Array],
  attr: {
    type: Object,
    default: () => {
      return { placeholder: '请选择' }
    }
  },
  formItem: {
    type: Object
  }
})
const emit = defineEmits(['update:fields', 'search'])
const visible = ref(false)
const popoverRef = ref(null)
const selected = ref(props.attr.multiple ? [] : null)

watch(() => props.fields, () => {
  selected.value = props.fields
})

const localOptions = computed(() => props.formItem?.options || [])

const selectText = computed(() => {
  if (props.attr.multiple && Array.isArray(props.fields)) {
    return props.fields.map(item => localOptions.value.find(option => option.value === item)).filter(Boolean).map(item => item.label).join('、')
  } else {
    return localOptions.value.find(option => option.value === props.fields)?.label
  }
})

function showList() {
  visible.value = true
}

function hideList() {
  visible.value = false
}

function selectItem(data) {
  if (props.attr.multiple) {
    if (!Array.isArray(selected.value)) {
      selected.value = []
    }
    selected.value = selected.value.includes(data.value) ? selected.value.filter(item => item !== data.value) : [...selected.value, data.value]
  } else {
    selected.value = data.value
    sureSelect()
  }
}

function sureSelect() {
  emit('update:fields', selected.value)
  emit('search')
  visible.value = false
  popoverRef.value.hide()
}

function clear() {
  selected.value = props.attr.multiple ? [] : null
  emit('update:fields', selected.value)
  emit('search')
}
</script>

<style scoped lang="scss">
@import "./search.scss";
</style>
